<template>
  <view class="login-container">
    <view class="logo-container">
      <image src="../../static/assets/lanqiao-logo.svg" mode="aspectFit" class="logo"></image>
    </view>
    <view class="input-container">
      <input type="number" placeholder="请输入手机号" v-model="phone" />
      <input type="password" placeholder="请输入密码" v-model="password" />
    </view>
    <button class="login-button" @click="login">登录</button>
    <navigator class="register-link" url="/pages/register/register">立即注册</navigator>
  </view>
</template>

<script>
import axios from 'axios';

export default {
  data() {
    return {
      phone: '',
      password: '',
      users: [] // 用于存储从接口获取的用户数据
    };
  },
  async onLoad() {
    try {
      const response = await axios.get('http://localhost:3000/user');
      this.users = response.data;
    } catch (error) {
      console.error('获取用户数据失败:', error);
      uni.showToast({
        title: '获取用户数据失败，请稍后重试',
        icon: 'none'
      });
    }
  },
  methods: {
    async login() {
      if (!this.phone ||!this.password) {
        uni.showToast({
          title: '手机号和密码不能为空',
          icon: 'none'
        });
        return;
      }
      const user = this.users.find(u => u.username === this.phone);
      if (!user) {
        uni.showToast({
          title: '账号未注册，请注册',
          icon: 'none'
        });
      } else if (user.password!== this.password) {
        uni.showToast({
          title: '密码错误，请重新输入',
          icon: 'none'
        });
      } else {
        // 保存登录状态到本地存储
        uni.setStorageSync('isLoggedIn', true); 
        try {
          // 发送请求到 people 接口，传递已登录状态
          await axios.post('http://localhost:3000/people', {
            userId: user.userid, // 假设接口需要用户 ID 来标识用户
            isLoggedIn: true
          });
          uni.showToast({
            title: '登录成功',
            icon:'success'
          });
          uni.switchTab({
            url: "/pages/me/me",
          });
        } catch (updateError) {
          console.error('更新用户登录状态失败:', updateError);
          uni.showToast({
            title: '登录时更新状态失败，请稍后重试',
            icon: 'none'
          });
        }
      }
    }
  }
};
</script>

<style scoped>
/* 整体背景样式 */
page {
  background-color: #f8f8f8;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
}


.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 50px;
  background-color: rgba(255, 255, 255, 0.9);
  border-radius: 20px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
  width: 400px;
  animation: fadeIn 0.5s ease-in-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.logo-container {
  margin-bottom: 30px;
}

.logo {
  width: 120px;
  height: 120px;
  filter: drop-shadow(0 0 10px rgba(0, 0, 0, 0.1));
}

.input-container {
  width: 100%;
  margin-bottom: 20px;
}

.input-container input {
  width: 350px;
  margin: auto;
  padding: 18px;
  margin-bottom: 20px;
  border: 2px solid #e0e0e0;
  border-radius: 10px;
  font-size: 16px;
  transition: border-color 0.3s ease;
}

.input-container input:focus {
  outline: none;
  border-color: #007AFF;
  box-shadow: 0 0 10px rgba(0, 122, 255, 0.2);
}

.login-button {
  width: 386px;
  padding: 5px;
  background-color: #007AFF;
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 18px;
  margin-bottom: 20px;
  cursor: pointer;
  transition: background-color 0.3s ease, transform 0.3s ease;
}

.login-button:hover {
  background-color: #0056b3;
  transform: scale(1.05);
}

.register-link {
  color: #333;
  text-decoration: none;
  transition: color 0.3s ease;
  font-size: 14px;
  text-align: center;
  width: 100%;
}

.register-link:hover {
  color: #007AFF;
}
</style>